package com.example.wastes.staff.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wastes.common.pojo.Staff;
import com.example.wastes.common.pojo.User;
import com.example.wastes.staff.mapper.StaffMapper;
import com.example.wastes.staff.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Autowired
    private StaffMapper staffMapper;
    @Override
    public List<Staff> findStaffName(String username) {
        return staffMapper.findStaffName(username);
    }

    @Override
    public Staff tologin(String username) {
        return staffMapper.tologin(username);
    }

    @Override
    public int saveStaff(String username, String password) {
        return staffMapper.saveStaff(username, password);
    }


    @Override
    public int upload2(Staff staff, MultipartFile fileUpload, HttpServletRequest request) {
        String originalFilename = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //重新生成文件名字
        originalFilename = UUID.randomUUID()+suffixName;
        //制定本地文件夹存储图片，写道需要保存的目录下
        String filePath = "D:\\ideaworkspeace\\images\\javaText\\";
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath+originalFilename));
            staff.setStaffimg("http://localhost:8080/wastes/" + originalFilename);
            return staffMapper.updateById(staff);
            //返回提示信息
        }catch(IOException e) {
            e.printStackTrace();
            throw new RuntimeException("头像上传失败");
        }
    }
}
